package com.xxz.controller;
import com.xxz.mapper.DepartmentMapper;
import com.xxz.mapper.EmployeeMapper;
import com.xxz.pojo.Department;
import com.xxz.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@CrossOrigin(origins = "http://localhost:8080")
public class MainController {
    @Autowired
    private DepartmentMapper departmentService;
    @Autowired
    private EmployeeMapper employeeService;

    //  信息展示  成功
    @RequestMapping("/emps")
    public String list(Model model){
        List<Employee> employees = employeeService.getAll();
        model.addAttribute("emps",employees);
        System.out.println("所有人员信息"+employees);
        return "emp/list";
    }

    //进到添加页面    成功  注意时间格式的定义
    @GetMapping("/emp")
    public String toAddpage(Model model){
        //查出所有部门
        List<Department> departments = departmentService.getDepartments();
        model.addAttribute("departments",departments);
        System.out.println("部门信息"+departments);
        return "emp/add";
    }

    //  实际添加功能    成功
    @PostMapping("/emp")
    public String Add(Employee employee){
        //添加操作
        employeeService.add(employee);
        return "redirect:/emps";
    }

    // 删除功能
    @RequestMapping("/delemp/{id}")
    public String Delete(@PathVariable("id") Integer id){
        employeeService.deleteEmployeeById(id);
        return "redirect:/emps";
    }


    // 去到修改页面
    @GetMapping("/emp/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){
        //查出原来的数据
        Employee employee = employeeService.getEmployeeById(id);
        model.addAttribute("emp",employee);
        //查出所有部门
        List<Department> departments = departmentService.getDepartments();
        System.out.println(departments.toString());
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    // 修改按钮点击实现
    @PostMapping("/update")
    public String Update(Employee employee){
        employeeService.update(employee);
        return "redirect:/emps";
    }

    // 返回首页功能
    @RequestMapping("/eee")
    public String To(){
        return  "dashboard";
    }
}




